package com.smart.upms.sys.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.core.starter.utils.CopyUtils;
import com.smart.upms.constant.CacheConstant;
import com.smart.upms.sys.entity.SysRole;
import com.smart.upms.sys.entity.SysRoleMenu;
import com.smart.upms.sys.mapper.SysRoleMapper;
import com.smart.upms.sys.mapper.SysRoleMenuMapper;
import com.smart.upms.sys.param.SysRoleMenuUpdateParam;
import com.smart.upms.sys.param.SysRoleParam;
import com.smart.upms.sys.param.SysRoleQueryParam;
import com.smart.upms.sys.result.SysRoleResult;
import com.smart.upms.sys.service.ISysRoleService;
import lombok.AllArgsConstructor;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色 服务实现类
 * </p>
 *
 * @author guxiaobai
 * @since 2018-12-05
 */
@Service
@AllArgsConstructor
public class SysRoleServiceImpl implements ISysRoleService {

    private final SysRoleMapper mapper;
    private final SysRoleMenuMapper roleMenuMapper;
    private final CacheManager cacheManager;


    @Override
    public Boolean save(SysRoleParam param) {
        SysRole entity = CopyUtils.copyObject(param, SysRole.class);
        int insert = mapper.insert(entity);
        return insert > 0;
    }

    @Override
    public Boolean update(SysRoleParam param) {
        SysRole entity = CopyUtils.copyObject(param, SysRole.class);
        int update = mapper.updateById(entity);
        return update > 0;

    }

    @Override
    public Boolean delete(Long id) {
        int delete = mapper.deleteById(id);
        return delete > 0;
    }

    @Override
    public SysRoleResult get(Long id) {
        SysRole entity = mapper.selectById(id);
        return CopyUtils.copyObject(entity, SysRoleResult.class);
    }

    @Override
    public List<SysRoleResult> list(SysRoleQueryParam param) {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        List<SysRole> entityList = mapper.selectList(queryWrapper);
        return CopyUtils.copyList(entityList, SysRoleResult.class);
    }

    @Override
    public Page<SysRoleResult> listPage(Page<SysRoleResult> page, SysRoleQueryParam param) {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        Page<SysRole> entityPage = new Page<>();
        entityPage.setSize(page.getSize());
        entityPage.setCurrent(page.getCurrent());
        entityPage.setAsc(page.ascs());
        entityPage.setDesc(page.descs());
        mapper.selectPage(entityPage, queryWrapper);
        Page<SysRoleResult> resultPage = CopyUtils.copyPage(entityPage, SysRoleResult.class);
        return resultPage;
    }

    @Override
    public List<SysRoleResult> findRolesByUserId(Long userId) {
        return mapper.findRolesByUserId(userId);
    }

    @Override
    @CacheEvict(value = CacheConstant.CacheMenu.ROLE_MENU_DETAILS, allEntries = true)
    public Boolean updateMenu(SysRoleMenuUpdateParam param) {
        QueryWrapper<SysRoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SysRoleMenu.ROLE_ID, param.getId());
        roleMenuMapper.delete(queryWrapper);
        if (CollectionUtil.isEmpty(param.getMenuIds())) {
            return Boolean.TRUE;
        }

        List<SysRoleMenu> roleMenuList = param.getMenuIds().stream()
                .map(menuId -> {
                    SysRoleMenu roleMenu = new SysRoleMenu();
                    roleMenu.setRoleId(param.getId());
                    roleMenu.setMenuId(menuId);
                    return roleMenu;
                }).collect(Collectors.toList());
        roleMenuList.forEach(sysRoleMenu -> {
            roleMenuMapper.insert(sysRoleMenu);
        });
        //清空用户缓存
        Objects.requireNonNull(cacheManager.getCache(CacheConstant.CacheUser.USER_DETAILS)).clear();
        return Boolean.TRUE;
    }

    @Override
    public List<SysRoleResult> listByDeptId(Long deptId) {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SysRole.DEPT_ID, deptId);
        List<SysRole> entityList = mapper.selectList(queryWrapper);
        return CopyUtils.copyList(entityList, SysRoleResult.class);
    }


}
